﻿
@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IPageModelCatche<List<LatestArticleItemModel>> _pageModelCatche

<section class="latest-articles-card">
    <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup Title="最新文章" Items="_items.Take(_dataCacheService.IsApp?9:12)" MoreLink="/search/?Types=Article">
        <MDCard Class="item" Href="@context.Url">
            <img class="image" src="@context.Image" alt="@context.Name" loading="lazy" />
            <div class="head">
                <div class="content">
                    <div class="name text-truncate-2 title-large">@context.Name</div>
                    <div class="brief text-truncate-3  label-large">@context.BriefIntroduction</div>
                </div>
                <div class="addinfo">
                    <div class="author">
                        @if (string.IsNullOrWhiteSpace(context.OriginalAuthor))
                        {
                            <img class="user-image" src="@context.UserImage" alt="@context.UserName" loading="lazy" />
                            <div class="name label-large">@context.UserName</div>
                        }
                        else
                        {
                            <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.IconChip Color="info" Icon="IconType.Carry" Text="搬运" />

                            <div class="name text-truncate-1 label-large">
                                作者：@context.OriginalAuthor
                            </div>
                        }
                    </div>
                    <div class="time label-large">@context.PublishTime</div>
                </div>
            </div>
        </MDCard>
    </CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup>
</section>

@code {

    List<LatestArticleItemModel> _items { get; set; } = new List<LatestArticleItemModel>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {
        try
        {
            //获取主页信息
            _items = await _pageModelCatche.GetCache("api/home/ListLatestArticles");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取主页最新文章列表失败");
        }
    }
}
